//-----------------------------------------------------------------------------------//
//              Windows Graphics Programming: Win32 GDI and DirectDraw               //
//                             ISBN  0-13-086985-6                                   //
//                                                                                   //
//  Written            by  Yuan, Feng                             www.fengyuan.com   //
//  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
//  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
//                                                                                   //
//  FileName   : main.cpp			                                                 //
//  Description: Main patcher program                                                //
//  Version    : 1.00.000, May 31, 2000                                              //
//-----------------------------------------------------------------------------------//

#define STRICT
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <assert.h>

#include "Patcher.h"

KPatcher patcher;

const char * FuncNames[] =
{
"AbortDoc",
"AbortPath",
"AddFontResourceA",
"AddFontResourceTracking",
"AddFontResourceW",
"AngleArc",
"AnimatePalette",
"Arc",
"ArcTo",
"BeginPath",
"BitBlt",
"CancelDC",
"CheckColorsInGamut",
"ChoosePixelFormat",
"Chord",
"CloseEnhMetaFile",
"CloseFigure",
"CloseMetaFile",
"ColorMatchToTarget",
"CombineRgn",
"CombineTransform",
"CopyEnhMetaFileA",
"CopyEnhMetaFileW",
"CopyMetaFileA",
"CopyMetaFileW",
"CreateBitmap",
"CreateBitmapIndirect",
"CreateBrushIndirect",
"CreateColorSpaceA",
"CreateColorSpaceW",
"CreateCompatibleBitmap",
// "CreateCompatibleDC",
"CreateDCA",
"CreateDCW",
"CreateDIBPatternBrush",
"CreateDIBPatternBrushPt",
"CreateDIBSection",
"CreateDIBitmap",
"CreateDiscardableBitmap",
"CreateEllipticRgn",
"CreateEllipticRgnIndirect",
"CreateEnhMetaFileA",
"CreateEnhMetaFileW",
"CreateFontA",
"CreateFontIndirectA",
"CreateFontIndirectW",
"CreateFontW",
"CreateHalftonePalette",
"CreateHatchBrush",
"CreateICA",
"CreateICW",
"CreateMetaFileA",
"CreateMetaFileW",
"CreatePalette",
"CreatePatternBrush",
"CreatePen",
"CreatePenIndirect",
"CreatePolyPolygonRgn",
"CreatePolygonRgn",
"CreateRectRgn",
"CreateRectRgnIndirect",
"CreateRoundRectRgn",
"CreateScalableFontResourceA",
"CreateScalableFontResourceW",
"CreateSolidBrush",
"DPtoLP",
"DeleteColorSpace",
"DeleteDC",
"DeleteEnhMetaFile",
"DeleteMetaFile",
"DeleteObject",
"DescribePixelFormat",
"DeviceCapabilitiesExA",
"DeviceCapabilitiesExW",
"DrawEscape",
"Ellipse",
"EndDoc",
"EndPage",
"EndPath",
"EnumEnhMetaFile",
"EnumFontFamiliesA",
"EnumFontFamiliesExA",
"EnumFontFamiliesExW",
"EnumFontFamiliesW",
"EnumFontsA",
"EnumFontsW",
"EnumICMProfilesA",
"EnumICMProfilesW",
"EnumMetaFile",
"EnumObjects",
"EqualRgn",
"Escape",
"ExcludeClipRect",
"ExtCreatePen",
"ExtCreateRegion",
"ExtEscape",
"ExtFloodFill",
"ExtSelectClipRgn",
"ExtTextOutA",
"ExtTextOutW",
"FillPath",
"FillRgn",
"FixBrushOrgEx",
"FlattenPath",
"FloodFill",
"FrameRgn",
"GdiAddFontResourceW",
"GdiAddGlsBounds",
"GdiAddGlsRecord",
"GdiCleanCacheDC",
"GdiComment",
"GdiConsoleTextOut",
"GdiConvertAndCheckDC",
"GdiConvertBitmap",
"GdiConvertBrush",
"GdiConvertDC",
"GdiConvertEnhMetaFile",
"GdiConvertFont",
"GdiConvertMetaFilePict",
"GdiConvertPalette",
"GdiConvertRegion",
"GdiConvertToDevmodeW",
"GdiCreateLocalEnhMetaFile",
"GdiCreateLocalMetaFilePict",
"GdiDeleteLocalDC",
"GdiDescribePixelFormat",
"GdiDllInitialize",
"GdiEntry1",
"GdiEntry10",
"GdiEntry11",
"GdiEntry12",
"GdiEntry13",
"GdiEntry14",
"GdiEntry15",
"GdiEntry2",
"GdiEntry3",
"GdiEntry4",
"GdiEntry5",
"GdiEntry6",
"GdiEntry7",
"GdiEntry8",
"GdiEntry9",
"GdiFixUpHandle",
"GdiFlush",
"GdiGetBatchLimit",
"GdiGetCharDimensions",
"GdiGetCodePage",
"GdiGetLocalBrush",
"GdiGetLocalDC",
"GdiGetLocalFont",
"GdiGetSpoolMessage",
"GdiInitSpool",
"GdiIsMetaFileDC",
"GdiIsMetaPrintDC",
"GdiPerf",
"GdiPlayDCScript",
"GdiPlayEMF",
"GdiPlayJournal",
"GdiPlayScript",
"GdiProcessSetup",
"GdiQueryFonts",
"GdiQueryTable",
"GdiReleaseLocalDC",
"GdiSetAttrs",
"GdiSetBatchLimit",
"GdiSetPixelFormat",
"GdiSetServerAttr",
"GdiSwapBuffers",
"GdiValidateHandle",
"GetArcDirection",
"GetAspectRatioFilterEx",
"GetBitmapBits",
"GetBitmapDimensionEx",
"GetBkColor",
"GetBkMode",
"GetBoundsRect",
"GetBrushOrgEx",
"GetCharABCWidthsA",
"GetCharABCWidthsFloatA",
"GetCharABCWidthsFloatW",
"GetCharABCWidthsW",
"GetCharWidth32A",
"GetCharWidth32W",
"GetCharWidthA",
"GetCharWidthFloatA",
"GetCharWidthFloatW",
"GetCharWidthInfo",
"GetCharWidthW",
"GetCharacterPlacementA",
"GetCharacterPlacementW",
"GetClipBox",
"GetClipRgn",
"GetColorAdjustment",
"GetColorSpace",
"GetCurrentObject",
"GetCurrentPositionEx",
"GetDCOrgEx",
"GetDIBColorTable",
"GetDIBits",
"GetDeviceCaps",
"GetDeviceGammaRamp",
"GetETM",
"GetEnhMetaFileA",
"GetEnhMetaFileBits",
"GetEnhMetaFileDescriptionA",
"GetEnhMetaFileDescriptionW",
"GetEnhMetaFileHeader",
"GetEnhMetaFilePaletteEntries",
"GetEnhMetaFilePixelFormat",
"GetEnhMetaFileW",
"GetFontData",
"GetFontLanguageInfo",
"GetFontResourceInfoW",
"GetGlyphOutline",
"GetGlyphOutlineA",
"GetGlyphOutlineW",
"GetGlyphOutlineWow",
"GetGraphicsMode",
"GetHFONT",
"GetICMProfileA",
"GetICMProfileW",
"GetKerningPairs",
"GetKerningPairsA",
"GetKerningPairsW",
"GetLogColorSpaceA",
"GetLogColorSpaceW",
"GetMapMode",
"GetMetaFileA",
"GetMetaFileBitsEx",
"GetMetaFileW",
"GetMetaRgn",
"GetMiterLimit",
"GetNearestColor",
"GetNearestPaletteIndex",
"GetObjectA",
"GetObjectType",
"GetObjectW",
"GetOutlineTextMetricsA",
"GetOutlineTextMetricsW",
"GetPaletteEntries",
"GetPath",
"GetPixel",
"GetPixelFormat",
"GetPolyFillMode",
"GetROP2",
"GetRandomRgn",
"GetRasterizerCaps",
"GetRegionData",
"GetRelAbs",
"GetRgnBox",
"GetStockObject",
"GetStretchBltMode",
"GetSystemPaletteEntries",
"GetSystemPaletteUse",
"GetTextAlign",
"GetTextCharacterExtra",
"GetTextCharset",
"GetTextCharsetInfo",
"GetTextColor",
"GetTextExtentExPointA",
"GetTextExtentExPointW",
"GetTextExtentPoint32A",
"GetTextExtentPoint32W",
"GetTextExtentPointA",
"GetTextExtentPointW",
"GetTextFaceA",
"GetTextFaceW",
"GetTextMetricsA",
"GetTextMetricsW",
"GetTransform",
"GetViewportExtEx",
"GetViewportOrgEx",
"GetWinMetaFileBits",
"GetWindowExtEx",
"GetWindowOrgEx",
"GetWorldTransform",
"IntersectClipRect",
"InvertRgn",
"LPtoDP",
"LineDDA",
"LineTo",
"MaskBlt",
"ModifyWorldTransform",
"MoveToEx",
"NamedEscape",
"OffsetClipRgn",
"OffsetRgn",
"OffsetViewportOrgEx",
"OffsetWindowOrgEx",
"PaintRgn",
"PatBlt",
"PathToRegion",
"Pie",
"PlayEnhMetaFile",
"PlayEnhMetaFileRecord",
"PlayMetaFile",
"PlayMetaFileRecord",
"PlgBlt",
"PolyBezier",
"PolyBezierTo",
"PolyDraw",
"PolyPatBlt",
"PolyPolygon",
"PolyPolyline",
"PolyTextOutA",
"PolyTextOutW",
"Polygon",
"Polyline",
"PolylineTo",
"PtInRegion",
"PtVisible",
"RealizePalette",
"RectInRegion",
"RectVisible",
"Rectangle",
"RemoveFontResourceA",
"RemoveFontResourceTracking",
"RemoveFontResourceW",
"ResetDCA",
"ResetDCW",
"ResizePalette",
"RestoreDC",
"RoundRect",
"SaveDC",
"ScaleViewportExtEx",
"ScaleWindowExtEx",
"SelectBrushLocal",
"SelectClipPath",
"SelectClipRgn",
"SelectFontLocal",
"SelectObject",
"SelectPalette",
"SetAbortProc",
"SetArcDirection",
"SetBitmapBits",
"SetBitmapDimensionEx",
"SetBkColor",
"SetBkMode",
"SetBoundsRect",
"SetBrushOrgEx",
"SetColorAdjustment",
"SetColorSpace",
"SetDIBColorTable",
"SetDIBits",
"SetDIBitsToDevice",
"SetDeviceGammaRamp",
"SetEnhMetaFileBits",
"SetFontEnumeration",
"SetGraphicsMode",
"SetICMMode",
"SetICMProfileA",
"SetICMProfileW",
"SetMagicColors",
"SetMapMode",
"SetMapperFlags",
"SetMetaFileBitsEx",
"SetMetaRgn",
"SetMiterLimit",
"SetPaletteEntries",
"SetPixel",
"SetPixelFormat",
"SetPixelV",
"SetPolyFillMode",
"SetROP2",
"SetRectRgn",
"SetRelAbs",
"SetStretchBltMode",
"SetSystemPaletteUse",
"SetTextAlign",
"SetTextCharacterExtra",
"SetTextColor",
"SetTextJustification",
"SetViewportExtEx",
"SetViewportOrgEx",
"SetVirtualResolution",
"SetWinMetaFileBits",
"SetWindowExtEx",
"SetWindowOrgEx",
"SetWorldTransform",
"StartDocA",
"StartDocW",
"StartPage",
"StretchBlt",
"StretchDIBits",
"StrokeAndFillPath",
"StrokePath",
"SwapBuffers",
"TextOutA",
"TextOutW",
"TranslateCharsetInfo",
"UnloadNetworkFonts",
"UnrealizeObject",
"UpdateColors",
"UpdateICMRegKey",
"UpdateICMRegKeyA",
"UpdateICMRegKeyW",
"WidenPath",
"bInitSystemAndFontsDirectoriesW",
"bMakePathNameW",
"cGetTTFFromFOT",
"gdiPlaySpoolStream"
};

void Call(int funcid)
{
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
{
	HPEN hPen = (HPEN) GetStockObject(BLACK_PEN);

	HMODULE hModule = GetModuleHandle(TEXT("GDI32.DLL"));

	for (int i=0; i<sizeof(FuncNames)/sizeof(FuncNames[0]); i++)
		//patcher.Patch(hModule, FuncNames[i], i, Call);
		patcher.Patch(hModule, FuncNames[i], i, Call, nullptr);

	// patcher.Patch(0, "SelectObject");
	// patcher.Patch(1, "CreateSolidBrush");
	// patcher.Patch(2, "DeleteObject");

	// HBRUSH hBrush = CreateSolidBrush(RGB(0xFF, 0xFF, 0));
	// SelectObject(GetDC(NULL), hBrush);
	// DeleteObject(hPen);

	OutputDebugString(TEXT("SetPixel\n"));
	SetPixel(GetDC(NULL), 1, 1, 0);

	OutputDebugString(TEXT("SetPixelV\n"));
	SetPixelV(GetDC(NULL), 1, 1, 0);

	OutputDebugString(TEXT("Begin MessageBox\n"));
	
	MessageBox(NULL, TEXT("How many calls"), TEXT("Hooked"), MB_OK);
	OutputDebugString(TEXT("End MessageBox\n"));

	return 0;
}